package jvm.gc;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 测试finalize()方法
 * <P> Date: 2022/3/21:13:54
 * <P> Description:
 */

public class CanRevivalTest implements Cloneable{

    static CanRevivalTest obj;

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize 中复活对象");
        obj = this;
    }

    @Override
    protected Object clone() {
        try {
           // return super.clone();
            CanRevivalTest canRevivalTest  = (CanRevivalTest) super.clone();
            return canRevivalTest;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        try {

            obj = new CanRevivalTest();
            obj = null;
            System.gc();
            System.out.println("第一次调用GC");
            Thread.sleep(2000);
            if (obj == null) {
                System.out.println("obj  is dead");
            } else {
                System.out.println("obj is keep alive");
            }


            obj = null;
            System.gc();
            System.out.println("第二次调用GC");
            Thread.sleep(2000);
            if (obj == null) {
                System.out.println("obj  is dead");
            } else {
                System.out.println("obj is keep alive");
            }

        } catch (Exception e) {

        }
    }
}
